<template>
  <a-modal
    title="新增开票"
    :visible="visible"
    :okText="$t('all.autotext49')"
    :canceltext="$t('all.autotext975')"
    :confirm-loading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-form-model
      ref="formRef"
      :model="form"
      :label-col="labelCol"
      :wrapper-col="wrapperCol"
      :rules="formRules"
    >
      <a-form-model-item prop="cashFlowType" label="入账方式">
        <a-select
          disabled
          placeholder="请选择入账方式"
          v-model="form.cashFlowType"
        >
          <a-select-option :value="0"> 合同入账 </a-select-option>
          <a-select-option :value="1"> 临床试验入账 </a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item prop="orderNumber" label="发票号">
        <a-input
          placeholder="请输入发票号码"
          v-model="form.orderNumber"
        ></a-input>
      </a-form-model-item>
      <a-form-model-item
        prop="proNo"
        v-if="form.cashFlowType === 1"
        label="临床试验编号"
      >
        <a-select v-model="form.proNo">
          <a-select-option
            v-for="item in codeList"
            :key="item.id"
            :value="item.no"
          >
            {{ item.no }}
          </a-select-option>
        </a-select>
      </a-form-model-item>
      <a-form-model-item label="开票金额（含税）" prop="invoiceMoney">
        <a-input-number
          :min="0"
          style="width: 270px"
          :precision="2"
          v-model="form.invoiceMoney"
          placeholder="请输入含税金额"
        />
        <span style="margin-left: 10px">{{ $t('all.autotext811') }}</span>
      </a-form-model-item>
      <a-form-model-item :label="$t('all.autotext1795')" prop="invoiceTime">
        <a-date-picker
          v-model="form.invoiceTime"
          style="width: 100%"
          valueFormat="YYYY-MM-DD"
        />
      </a-form-model-item>
    </a-form-model>
  </a-modal>
</template>

<script>
import { saveContractCashFlow, getListByBusinessCode } from '@/api/entry/entry'

export default {
  data() {
    return {
      visible: false,
      businessCode: '',
      labelCol: { span: 7 },
      wrapperCol: { span: 15 },
      formRules: {
        invoiceMoney: [
          {
            required: true,
            message: this.$t('all.autotext1740'),
            trigger: 'blur'
          },
          { validator: this.validatePass, trigger: 'blur' }
        ],
        orderNumber: [
          { required: true, message: '请输入发票号', trigger: 'blur' }
        ],
        proNo: [
          { required: true, message: '请选择临床试验编号', trigger: 'blur' }
        ],
        cashFlowType: [
          { required: true, message: '请选择入账方式', trigger: 'change' }
        ],
        invoiceTime: [
          { required: true, message: '请选择开票日期', trigger: 'change' }
        ]
      },
      form: {
        orderNumber: '', // 发票号
        invoiceTime: '',
        cashFlowType: '', // 合同入账
        proNo: '', // 临床试验编号
        invoiceMoney: '' // 开票金额
      },
      confirmLoading: false,
      codeList: []
    }
  },
  methods: {
    open(code, cashFlowType) {
      this.form.cashFlowType = cashFlowType
      this.visible = true
      this.businessCode = code
      getListByBusinessCode({
        businessCode: code,
        classify: this.$store.state.user.classify
      }).then((res) => {
        this.codeList = res
      })
    },

    handleOk() {
      this.$refs.formRef.validate(async (valid) => {
        if (!valid) return
        this.confirmLoading = true
        try {
          await saveContractCashFlow({
            businessCode: this.businessCode,
            classify: this.$store.state.user.classify,
            ...this.form
          })
          this.$emit('refresh')
          this.handleCancel()
        } finally {
          this.confirmLoading = false
        }
      })
    },
    handleCancel() {
      this.$refs.formRef.resetFields()
      this.visible = false
    },
    validatePass(rule, value, callback) {
      if (!value) {
        callback()
      } else {
        if (value <= 0) {
          callback(new Error(this.$t('all.autotext1741')))
        } else {
          callback()
        }
      }
    }
  }
}
</script>
